/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.plant.action;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.plant.entity.UgcFaq;
import com.plant.entity.UgcFaqAnswer;
import com.plant.entity.UgcUploadImages;
import com.plant.entity.UgcUploadImagesTags;
import com.plant.entity.UgcUser;
import com.plant.service.FuzzySearchService;
import com.plant.service.HelpService;
import com.plant.service.UserServic;
import com.plant.tool.BuildString;
import com.plant.tool.DateUtils;
import com.plant.tool.ListMapSort;
import com.plant.tool.NowTimetool;
import com.plant.tool.SendMessage;
/**
 * 			 涉及到的表：表1:ugc_faq;表2：ugc_faq_answer
 * 			  --下面是求助的一些统计，1统计我的条数 2，查看求助的一些信息，3.用户发布求助
 */

@Controller
@RequestMapping(value = "/help")
public class HelpAction {

	@Resource(name = "helpServiceImpl")//spring 注入依赖管理 注入管理接口service
	private HelpService helpService;

	@Resource(name = "userInfoImlp")  //---用户个人方法
	private UserServic userServic;

	@Resource(name = "fuzzySearchServiceImpl")//--模糊搜索的接口方法
	private FuzzySearchService fuzzySearchService;

	@RequestMapping(value="helpcount")    //---1.求助的的各个栏目的计数统计
	@ResponseBody
	public Map<Object,String> helpCount(HttpServletResponse response,UgcUser uf,String secret,String mdsc){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,String> map=new HashMap<Object, String>();
		if(secret==null||mdsc==null||uf.getId()==null){
			map.put("state", "0");
			map.put("message", "参数为空");
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(secret+"flower_duigu2016");
			if(mdsc.endsWith(mdc)){
				List<Long> list=new ArrayList<Long>();
				list=helpService.helpCount(uf);//list集合条数1.我的求助的条数mycount 2.好友求助条数friendcount 3.陌生人求助条数notfriendcount 4.我的回答answercount
				for (int i = 0; i < list.size(); i++) {
					map.put("mycount", list.get(0).toString());
					map.put("friendcount", list.get(1).toString());
					map.put("notfriendcount", list.get(2).toString());
					map.put("answercount", list.get(3).toString());
				}
				return map;
			}else{
				map.put("state", "0");
				map.put("message", "秘钥错误");
				return map;
			}
		}
	} 

	@RequestMapping(value="myfaq")     //----2.列表显示我的求助 
	@ResponseBody
	public Map<Object,Object> myfaq(HttpServletResponse response,UgcUser uf,String secret,String mdsc,String time,Integer page){
		response.setHeader("access-control-allow-origin","*");
		List<Object[]> list=new ArrayList<Object[]>();
		List<Object[]> list4=new ArrayList<Object[]>();
		List<Object> list1=new ArrayList<Object>();
		Map<Object,Object> map1=new HashMap<Object, Object>();
		if(secret==null||mdsc==null||time==null||page==null||uf.getId()==null){
			map1.put("message", "参数为空");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(secret+"flower_duigu2016");
			if(mdsc.endsWith(mdc)){
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				DateUtils du=new DateUtils();
				list=helpService.myFaq(uf,page,time);
				list4=helpService.myFaq(uf,page+1,time);
				if(list4.size()==0){
					map1.put("hasmore", 0);
				}else{
					map1.put("hasmore", 1);
				} 
				for (int i = 0; i < list.size(); i++) {
					Map<Object,Object> map=new HashMap<Object, Object>();
					Object[] obj = list.get(i);
					Long friendanswer=helpService.faqcount(uf, obj[0]);
					Long strangeranswer=helpService.nofriendcount(uf, obj[0]);
					map.put("url", helpService.oneurl(uf, obj[0]));
					map.put("id", obj[0]);
					map.put("title", obj[1]);
					String t=sim.format(obj[2]);
					long l=du.getUnixStamps(t);
					map.put("createtime",du.convertTimeToFormat(l));
					map.put("friendanswer", friendanswer);
					map.put("strangeranswer", strangeranswer);
					list1.add(map);
				}
				map1.put("message", "参数为空");
				map1.put("list", list1);
				map1.put("state", 1);
				return map1;
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	}



	@RequestMapping(value = "frendraq")   //----3显示好友求助-- 查看显示
	@ResponseBody
	public Map<Object,Object> frendfaq(HttpServletResponse response,UgcUser uf,String secret,String mdsc,Integer page,String time){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map1=new HashMap<Object, Object>();
		if(secret==null||mdsc==null||page==null||time==null||uf.getId()==null){
			map1.put("message", "参数为空");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(secret+"flower_duigu2016");
			if(mdsc.endsWith(mdc)){
				//	   				uf.setId(35);
				//	   				String times="2016-10-28 19:05:52";
				List<Object[]> list=new ArrayList<Object[]>();
				List<Object[]> list4=new ArrayList<Object[]>();
				List<Object> list1=new ArrayList<Object>();
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				DateUtils du=new DateUtils();
				List<Object> list3=new ArrayList<Object>();
				List<Object> list2=new ArrayList<Object>();
				list2=helpService.GetFriendUidByA(uf);
				list3=helpService.GetFriendUidByR(uf);
				list3.addAll(list2);
				int len=list3.size();
				if(len==0){
					System.out.println("你没有好友");
				}else{
					Object ids=StringUtils.join(list3.toArray(),",");
					list=helpService.frendFaq(ids,page,time);
					list4=helpService.frendFaq(ids,page+1,time);
					if(list4.size()==0){
						map1.put("hasmore", 0);
					}else{
						map1.put("hasmore", 1);
					}
					for (int i = 0; i < list.size(); i++) {
						Map<Object,Object> map=new HashMap<Object, Object>();
						Object[] obj = list.get(i);
						Long answercount=helpService.friendfaqcount(uf, obj[0]);
						map.put("myanswer", helpService.friendfaqmyanswer(uf, obj[0]));
						map.put("url", helpService.oneurl(uf, obj[0]));
						map.put("faqid", obj[0]);
						map.put("uid", obj[1]);
						map.put("title", obj[2]);
						String t=sim.format(obj[3]);
						long l=du.getUnixStamps(t);
						map.put("createtime",du.convertTimeToFormat(l));
						map.put("nickname", obj[4]);
						map.put("photo", obj[5]);
						map.put("answercount", answercount);
						list1.add(map);
					}
				}
				map1.put("message", "查询成功");
				map1.put("state", 0);
				map1.put("list", list1);
				return map1;
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	}


	@RequestMapping(value = "strangerfaq")//---4.显示陌生人求助 查看显示
	@ResponseBody
	public Map<Object,Object> strangerfaq(HttpServletResponse response,UgcUser uf,String secret,String mdsc,Integer page,String time){
		response.setHeader("access-control-allow-origin","*");
		Map<Object, Object> map1=new HashMap<Object, Object>();
		List<Object[]> list=new ArrayList<Object[]>();
		List<Object[]> list4=new ArrayList<Object[]>();
		List<Object> list1=new ArrayList<Object>();
		if(secret==null||mdsc==null||page==null||time==null||uf.getId()==null){
			map1.put("message", "参数为空");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(secret+"flower_duigu2016");
			if(mdsc.endsWith(mdc)){
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				DateUtils du=new DateUtils();
				//					uf.setId(94);
				//					String times="2016-09-29 18:57:47";
				List<Object> list3=new ArrayList<Object>();
				List<Object> list2=new ArrayList<Object>();
				list2=helpService.GetFriendUidByA(uf);
				list3=helpService.GetFriendUidByR(uf);
				list3.addAll(list2);
				int len=list3.size();
				if(len==0){
					Object ids=uf.getId().toString();
					list=helpService.strangerFaq(ids,uf,page,time);
					list4=helpService.strangerFaq(ids,uf,page+1,time);
					if(list4.size()==0){
						map1.put("hasmore", 0);
					}else{
						map1.put("hasmore", 1);
					}
					for (int i = 0; i < list.size(); i++) {
						Map<Object,Object> map=new HashMap<Object, Object>();
						Object[] obj = list.get(i);
						map.put("faqid", obj[0]);
						Long answercount=helpService.friendfaqcount(uf, obj[0]);
						map.put("myanswer", helpService.friendfaqmyanswer(uf, obj[0]));
						map.put("url", helpService.oneurl(uf, obj[0]));
						map.put("uid", obj[1]);
						map.put("title", obj[2]);
						String t=sim.format(obj[3]);
						long l=du.getUnixStamps(t);
						map.put("create", du.convertTimeToFormat(l));
						map.put("nickname", obj[4]);
						map.put("photo", obj[5]);
						map.put("answercount", answercount);
						list1.add(map);
					}
				}else{
					Object ids=StringUtils.join(list3.toArray(),",");
					list=helpService.strangerFaq(ids,uf,page,time);
					list4=helpService.strangerFaq(ids,uf,page+1,time);
					if(list4.size()==0){
						map1.put("hasmore", 0);
					}else{
						map1.put("hasmore", 1);
					}
					for (int i = 0; i < list.size(); i++) {
						Map<Object,Object> map=new HashMap<Object, Object>();
						Object[] obj = list.get(i);
						map.put("faqid", obj[0]);
						Long answercount=helpService.friendfaqcount(uf, obj[0]);
						map.put("myanswer", helpService.friendfaqmyanswer(uf, obj[0]));
						map.put("url", helpService.oneurl(uf, obj[0]));
						map.put("uid", obj[1]);
						map.put("title", obj[2]);
						String t=sim.format(obj[3]);
						long l=du.getUnixStamps(t);
						map.put("create", du.convertTimeToFormat(l));
						map.put("nickname", obj[4]);
						map.put("photo", obj[5]);
						map.put("answercount", answercount);
						list1.add(map);
					}
				}
				map1.put("message", "查询成功");
				map1.put("state", 0);
				map1.put("list", list1);
				return map1;
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	}


	@RequestMapping(value = "myanswner") //---5.显示我的回答-- 查看显示
	@ResponseBody
	public Map<Object,Object> myanswner(HttpServletResponse response,UgcUser uf,String secret,String mdsc,Integer page,String time){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map1=new HashMap<Object, Object>();
		List<Object[]> list=new ArrayList<Object[]>();
		List<Object[]> list4=new ArrayList<Object[]>();
		List<Object> list1=new ArrayList<Object>();
		if(secret==null||mdsc==null||page==null||time==null||uf.getId()==null){
			map1.put("message", "请求参数错误");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(secret+"flower_duigu2016");
			if(mdsc.endsWith(mdc)){
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				DateUtils du=new DateUtils();
				list=helpService.myanswer(uf,page,time);
				list4=helpService.myanswer(uf,page+1,time);
				if(list4.size()==0){
					map1.put("hasmore", 0);
				}else{
					map1.put("hasmore", 1);
				}
				for (int i = 0; i < list.size(); i++) {
					Map<Object,Object> map=new HashMap<Object, Object>();
					Object[] obj = list.get(i);
					map.put("contenid", obj[0]);
					String t=sim.format(obj[1]);
					long l=du.getUnixStamps(t);
					map.put("createtime", du.convertTimeToFormat(l));
					map.put("content", obj[2]);
					map.put("faid", obj[3]);
					Long answercount=helpService.friendfaqcount(uf, obj[3]);
					map.put("url", helpService.oneurl(uf, obj[3]));
					map.put("title", obj[4]);
					map.put("uid", obj[5]);
					map.put("nickname", obj[6]);
					map.put("photo", obj[7]);
					map.put("answercount", answercount);
					list1.add(map);
				}
				map1.put("message", "查询成功");
				map1.put("state", 1);
				map1.put("list", list1);
				return map1;
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	}


	@RequestMapping(value = "myanswer1_1_2") //---5.显示我的回答1.1.2
	@ResponseBody
	public Map<String,Object> myanswnerI(Integer user_id,String time,HttpServletResponse response,UgcUser uf,String secret,String pust_val,Integer page){
		response.setHeader("access-control-allow-origin","*");
		Map<String,Object> map1=new HashMap<String, Object>();
		if(secret==null||pust_val==null||page==null||time==null){
			map1.put("message", "请求参数错误");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				//	   		   Integer user_id=224;
				//	   		   String time="2016-12-30 11:50:59";
				uf.setId(user_id);
				List<Object[]> list=new ArrayList<Object[]>();
				List<Object[]> list4=new ArrayList<Object[]>();
				List<Object> answerids=new ArrayList<Object>();
				List<Map<String,Object>> list1=new ArrayList<Map<String,Object>>();
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				DateUtils du=new DateUtils();
				list=helpService.myanswerI(uf,page,time);
				list4=helpService.myanswerI(uf,page+1,time);
				int len=list4.size();
				if(len==0){
					map1.put("hasmore", 0);
				}else{
					map1.put("hasmore", 1);
				}
				for (int i = 0; i < list.size(); i++) {
					Map<String,Object> map=new HashMap<String, Object>();
					Object[] obj = list.get(i);
					map.put("contenid", obj[0]);
					String t=sim.format(obj[1]);
					long l=du.getUnixStamps(t);
					map.put("createtime", du.convertTimeToFormat(l));
					map.put("content", obj[2]);
					map.put("faid", obj[3]);
					map.put("title", obj[4]);
					map.put("uid", obj[5]);
					map.put("nickname", obj[6]);
					map.put("photo", obj[7]);
					answerids=fuzzySearchService.AllAnswerIds(obj[3]);
					int lens=answerids.size();
					if(lens==0){
						map.put("count_like", 0);
					}else{
						Object ids=StringUtils.join(answerids.toArray(),",");
						map.put("count_like", fuzzySearchService.CountAllFaqAnswerCount(ids));
					}
					list1.add(map);
				}
				ListMapSort.Sort(list1);
				map1.put("message", "查询成功");
				map1.put("state", 1);
				map1.put("list", list1);
				return map1;
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	}



	@RequestMapping(value = "answer_help")//---6回答求助
	@ResponseBody
	public Map<Object,Object> AnswerHelp(String user_name,UgcUser uf,Integer to_user_id,HttpServletRequest request,UgcFaqAnswer ufa,Integer to_faq_id,Integer user_id,String content,HttpServletResponse response,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map1=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||to_faq_id==null||user_id==null||content==null||to_user_id==null){
			map1.put("message", "参数为空");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String s=sim.format(new Date());
				GetIp getip=new GetIp();
				String ip=getip.getIp2(request);
				ufa.setContent(content);
				ufa.setCreateTime(Timestamp.valueOf(s));
				ufa.setIp(ip);
				ufa.setUserId(user_id);
				ufa.setToFaqId(to_faq_id);
				boolean b=helpService.AddFaqAnswer(ufa);
				if(b){
					if(to_user_id.equals(user_id)){
						map1.put("mes", "回答求助成功");
						map1.put("state", 1);
					}else{
						uf.setId(to_user_id);
						UgcUser u=(UgcUser) userServic.OneUser(uf);
						if(u==null){
							map1.put("mes", "用户为空");
						}else{
							String deviceToken=u.getDiviceToken();
							if(deviceToken.isEmpty()){
								map1.put("mes", "用户TOKEN为空");
							}else{
								SendMessage send=new SendMessage();
								String mes=send.IosSendMessageFaqAnswer(deviceToken,to_faq_id);
								map1.put("mes", mes);
							}
							map1.put("mes", "回答求助成功");
							map1.put("state", 1);
						}
					}
					return map1;
				}else{
					map1.put("message", "回答求助失败");
					map1.put("state", 0);
					return map1;
				}
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	}


	@RequestMapping(value = "weixin_answer_help")//微信回答求助
	@ResponseBody
	public Map<Object,Object> AnswerHelpWithWeiXin(Integer user_id,Integer to_faq_id,String content,Integer fromplatform,Integer to_user_id,UgcUser uf,HttpServletRequest request,UgcFaqAnswer ufa,HttpServletResponse response,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map1=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||to_faq_id==null||user_id==null||content==null||to_user_id==null){
			map1.put("message", "参数为空");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				//	        Integer fromplatform=1;
				//	        Integer to_user_id=145;
				//	        String content="c";
				//	        Integer to_faq_id=162;
				//	        Integer user_id=229;
				if(fromplatform==null){
					ufa.setFromPlatform(0); 
				}else{
					ufa.setFromPlatform(fromplatform);
				}
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String s=sim.format(new Date());
				GetIp getip=new GetIp();
				String ip=getip.getIp2(request);
				ufa.setContent(content);
				ufa.setCreateTime(Timestamp.valueOf(s));
				ufa.setIp(ip);
				ufa.setUserId(user_id);
				ufa.setToFaqId(to_faq_id);
				boolean b=helpService.AddFaqAnswer(ufa);
				if(b){
					if(to_user_id.equals(user_id)){
						map1.put("mes", "回答求助成功");
						map1.put("state", 1);
					}else{
						uf.setId(to_user_id);
						UgcUser u=(UgcUser) userServic.OneUser(uf);
						if(u==null){
							map1.put("mes", "用户为空");
						}else{
							String deviceToken=u.getDiviceToken();
							if(deviceToken==null){
								map1.put("mes", "用户TOKEN为空");
							}else{
								SendMessage send=new SendMessage();
								String mes=send.IosSendMessageFaqAnswer(deviceToken,to_faq_id);
								map1.put("mes", mes);
							}
							map1.put("mes", "回答求助成功");
							map1.put("state", 1);
						}
					}
					return map1;
				}else{
					map1.put("message", "回答求助失败");
					map1.put("state", 0);
					return map1;
				}
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	}

	@RequestMapping(value = "help_info") //---7求助的基本信息
	@ResponseBody
	public Map<Object,Object> HelpInfo(UgcUser u,UgcFaq uf,Integer help_content_id,HttpServletRequest request,HttpServletResponse response,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||help_content_id==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				//			   		uf.setId(414);
				uf.setId(help_content_id);
				List<Object[]> list1=new ArrayList<Object[]>();
				List<Object[]> list2=new ArrayList<Object[]>();
				List<Object[]> list5=new ArrayList<Object[]>();
				List<Object[]> list7=new ArrayList<Object[]>();
				List<Object> list3=new ArrayList<Object>();
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				DateUtils du=new DateUtils();
				list1=helpService.HelpInfo(uf);
				if(list1.size()==0){
					map.put("error", "该求助已经被删除");
				}else{
					for (int i = 0; i < list1.size(); i++) {
						Object[] obj=list1.get(i);
						map.put("id_key", obj[0]);
						map.put("user_id", obj[1]);
						if(obj[2]==null){
							int[] remind_user_id={};
							map.put("remind_who", remind_user_id);
						}else{
							String string=obj[2].toString();
							String [] stringArr= string.split(",");
							for (int j = 0; j < stringArr.length; j++) {
								Map<Object,Object> map3=new HashMap<Object, Object>();
								UgcUser user=null;
								Integer uid=Integer.parseInt(stringArr[j]);
								u.setId(uid);
								user=(UgcUser) helpService.PartUserInf(u);
								map3.put("user_id", user.getId());
								map3.put("nickname", user.getNickname());
								map3.put("photo", user.getPhoto());
								list3.add(map3);
							}
							map.put("remind_user_id", list3);//--将字符串变成数组
						}
						map.put("title", obj[3]);
						map.put("content", obj[4]);
						String t=sim.format(obj[5]);
						long l=du.getUnixStamps(t);
						map.put("create_time",du.TopicCommentTime(l));
						map.put("nickname", obj[6]);
						map.put("photo", obj[7]);
						map.put("answer_total", helpService.FaqAnswerCount(obj[0]));
						list2=helpService.HelpInfImagesListS(obj[0]);
						map.put("images_list", list2);
						list7=helpService.HelpInfImagesList(obj[0]);
						Map<Object,Object> map6=new HashMap<Object, Object>();
						for (int j = 0; j < list7.size(); j++) {
							Object[] obj7=list7.get(j);
							list5=helpService.HelpInfImagesTags(obj7[2], obj7[0]);
							List<Object> list6=new ArrayList<Object>();
							for (int k = 0; k < list5.size(); k++) {
								Map<Object,Object> map5=new HashMap<Object, Object>();
								Object[] obj5=list5.get(k);
								map5.put("id_key", obj5[0]);
								map5.put("x", obj5[1]);
								map5.put("y", obj5[2]);
								map5.put("rediret", obj5[3]);
								map5.put("text", obj5[4]);
								list6.add(map5);
							}
							map6.put("image"+j+"",list6);
						}
						map.put("images_tags_list",map6);
					}
				}
				map.put("message", "查询成功");	
				map.put("state", 1);	
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 0);
				return map;
			}
		}
	}


	@RequestMapping(value = "help_info_1_1_2") //7求助基本信息
	@ResponseBody
	public Map<Object,Object> HelpInfoI(UgcUser u,UgcFaq uf,Integer help_content_id,HttpServletRequest request,HttpServletResponse response,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||help_content_id==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				//uf.setId(414);
				uf.setId(help_content_id);
				List<Object[]> list1=new ArrayList<Object[]>();
				List<Object[]> list2=new ArrayList<Object[]>();
				List<Object[]> list5=new ArrayList<Object[]>();
				List<Object[]> list7=new ArrayList<Object[]>();
				List<Object> list3=new ArrayList<Object>();
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				DateUtils du=new DateUtils();
				list1=helpService.HelpInfo(uf);
				if(list1.size()==0){
					map.put("error", "该求助已经被删除");
				}else{
					for (int i = 0; i < list1.size(); i++) {
						Object[] obj=list1.get(i);
						map.put("id_key", obj[0]);
						map.put("user_id", obj[1]);
						if(obj[2]==null){
							int[] remind_user_id={};
							map.put("remind_who", remind_user_id);
						}else{
							String string=obj[2].toString();
							String [] stringArr= string.split(",");
							for (int j = 0; j < stringArr.length; j++) {
								Map<Object,Object> map3=new HashMap<Object, Object>();
								UgcUser user=null;
								Integer uid=Integer.parseInt(stringArr[j]);
								u.setId(uid);
								user=(UgcUser) helpService.PartUserInf(u);
								map3.put("user_id", user.getId());
								map3.put("nickname", user.getNickname());
								map3.put("photo", user.getPhoto());
								list3.add(map3);
							}
							map.put("remind_user_id", list3);//--将字符串变成数组
						}
						map.put("title", obj[3]);
						map.put("content", obj[4]);
						String t=sim.format(obj[5]);
						long l=du.getUnixStamps(t);
						map.put("create_time",du.TopicCommentTime(l));
						map.put("nickname", obj[6]);
						map.put("photo", obj[7]);
						map.put("answer_total", helpService.FaqAnswerCount(obj[0]));
						list2=helpService.HelpInfImagesListS(obj[0]);
						map.put("images_list", list2);
						list7=helpService.HelpInfImagesList(obj[0]);
						Map<Object,Object> map6=new HashMap<Object, Object>();
						for (int j = 0; j < list7.size(); j++) {
							Object[] obj7=list7.get(j);
							list5=helpService.HelpInfImagesTags(obj7[2], obj7[0]);
							List<Object> list6=new ArrayList<Object>();
							for (int k = 0; k < list5.size(); k++) {
								Map<Object,Object> map5=new HashMap<Object, Object>();
								Object[] obj5=list5.get(k);
								map5.put("id_key", obj5[0]);
								map5.put("x", obj5[1]);
								map5.put("y", obj5[2]);
								map5.put("rediret", obj5[3]);
								map5.put("text", obj5[4]);
								list6.add(map5);
							}
							map6.put("image"+j+"",list6);
						}
						map.put("images_tags_list",map6);
					}
				}
				map.put("message", "查询成功");	
				map.put("state", 1);	
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 0);
				return map;
			}
		}
	}




	@RequestMapping(value = "answer_help_list")//---8求助的回答列表（每页10条）
	@ResponseBody
	public Map<Object,Object> AnswerHelpList(Integer help_content_id,Integer page_num,HttpServletRequest request,UgcFaqAnswer ufa,HttpServletResponse response,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map1=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||help_content_id==null){
			map1.put("message", "参数为空");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				ufa.setToFaqId(help_content_id);
				List<Object[]> list=new ArrayList<Object[]>();
				List<Object[]> list2=new ArrayList<Object[]>();
				List<Object> list1=new ArrayList<Object>();
				NowTimetool nowtime=new NowTimetool();
				DateUtils du=new DateUtils();
				String time=nowtime.nowdate();
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				if(page_num==null){
					list=helpService.AnswerHelpList(ufa, 1, time);
					list2=helpService.AnswerHelpList(ufa, 2, time);
					if(list2.size()==0){
						map1.put("hasmore", 0);
					}else{
						map1.put("hasmore", 1);
					}
					for (int i = 0; i < list.size(); i++) {
						Map<Object,Object> map=new HashMap<Object, Object>();
						Object[] obj=list.get(i);
						map.put("id_key", obj[0]);
						map.put("user_id", obj[1]);
						map.put("content", obj[2]);
						map.put("to_faq_id", obj[3]);
						String t=sim.format(obj[4]);
						long l=du.getUnixStamps(t);
						map.put("add_time",du.TopicCommentTime(l));
						map.put("nickname", obj[5]);
						if(obj[6]==null){
							map.put("user_photo","");
						}else{
							map.put("user_photo", obj[6]);
						}
						list1.add(map);
					}
					map1.put("content", list1);
					map1.put("message", "查询成功");
					map1.put("state", 1);
					return map1;
				}else{
					list=helpService.AnswerHelpList(ufa, page_num, time);
					list2=helpService.AnswerHelpList(ufa, page_num+1, time);
					if(list2.size()==0){
						map1.put("hasmore", 0);
					}else{
						map1.put("hasmore", 1);
					}
					for (int i = 0; i < list.size(); i++) {
						Map<Object,Object> map=new HashMap<Object, Object>();
						Object[] obj=list.get(i);
						map.put("id_key", obj[0]);
						map.put("user_id", obj[1]);
						map.put("content", obj[2]);
						map.put("to_faq_id", obj[3]);
						String t=sim.format(obj[4]);
						long l=du.getUnixStamps(t);
						map.put("add_time",du.TopicCommentTime(l));
						map.put("nickname",obj[5]);
						if(obj[6]==null){
							map.put("user_photo", "");
						}else{
							map.put("user_photo", obj[6]);
						}
						list1.add(map);
					}
					map1.put("content", list1);
					map1.put("message", "查询成功");
					map1.put("state", 1);
					return map1;
				}
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	}



	@RequestMapping(value = "answer_help_list_1_1_2")//---8求助的回答列表（每页10条）--1.1.2版本
	@ResponseBody
	public Map<Object,Object> AnswerHelpListI(String first_page_time,Integer user_id,Integer help_content_id,Integer page_num,HttpServletRequest request,UgcFaqAnswer ufa,HttpServletResponse response,String secret,String pust_val){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map1=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||help_content_id==null||first_page_time==null||page_num==null){
			map1.put("message", "参数为空");
			map1.put("state", 0);
			return map1;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				//			   ufa.setToFaqId(505);
				//			   Integer user_id=147;
				//			   			   String first_page_time="2016-12-24 01:47:25";
				ufa.setToFaqId(help_content_id);
				List<Object[]> list=new ArrayList<Object[]>();
				List<Object[]> list2=new ArrayList<Object[]>();
				List<Object> list1=new ArrayList<Object>();
				DateUtils du=new DateUtils();
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				list=helpService.AnswerHelpList(ufa, page_num, first_page_time);
				list2=helpService.AnswerHelpList(ufa, page_num+1, first_page_time);
				if(list2.size()==0){
					map1.put("hasmore", 0);
				}else{
					map1.put("hasmore", 1);
				}
				for (int i = 0; i < list.size(); i++) {
					Map<Object,Object> map=new HashMap<Object, Object>();
					Object[] obj=list.get(i);
					map.put("id_key", obj[0]);
					map.put("user_id", obj[1]);
					map.put("content", obj[2]);
					map.put("to_faq_id", obj[3]);
					String t=sim.format(obj[4]);
					long l=du.getUnixStamps(t);
					map.put("add_time",du.TopicCommentTime(l));
					map.put("nickname",obj[5]);
					if(obj[6]==null){
						map.put("user_photo", "");
					}else{
						map.put("user_photo", obj[6]);
					}
					Integer to_user_id=Integer.parseInt(obj[1].toString());
					Integer to_content_id=Integer.parseInt(obj[0].toString());
					boolean b=helpService.CheckFaqAnswerLikes(user_id, to_user_id, to_content_id);
					if(b){
						map.put("islike", "1");
					}else{
						map.put("islike", "0");
					}
					map.put("like_count", helpService.FaqLikeCount(obj[0]).toString());
					if(obj[7]==null){
						map.put("from_platform", 0);
					}else{
						if(obj[7].equals(0)){
							map.put("from_platform", 0);
						} else{
							map.put("from_platform", 1);
						}
					}
					list1.add(map);
				}
				map1.put("content", list1);
				map1.put("message", "查询成功");
				map1.put("state", 1);
				return map1;
			}else{
				map1.put("message", "秘钥错误");
				map1.put("state", 0);
				return map1;
			}
		}
	}

	@RequestMapping(value = "/newfaq")//---9求助推送所有用户
	@ResponseBody
	public Map<Object,Object> newfaqsI(UgcUser uf,HttpServletResponse response,UgcFaq fq,Integer user_id,String pust_val,String secret,HttpServletRequest request,@RequestParam(value="remind_who[]",required=false) String[] id,@RequestParam(value = "file", required = false) MultipartFile[] myfiles,ModelMap model,
			@RequestParam(value="tag0[]",required=false) String[] tag0,@RequestParam(value="tag1[]",required=false) String[] tag1,@RequestParam(value="tag2[]",required=false) String[] tag2,@RequestParam(value="tag3[]",required=false) String[] tag3,
			@RequestParam(value="tag4[]",required=false) String[] tag4,@RequestParam(value="tag5[]",required=false) String[] tag5,@RequestParam(value="tag6[]",required=false) String[] tag6,@RequestParam(value="tag7[]",required=false) String[] tag7,@RequestParam(value="tag8[]",required=false) String[] tag8){
		response.setHeader("access-control-allow-origin","*");
		Map<Object,Object> map=new HashMap<Object, Object>();
		if(secret==null||pust_val==null||fq.getTitle()==null||fq.getContent()==null){
			map.put("message", "参数为空");
			map.put("state", 0);
			return map;
		}else{
			Md md=new Md();
			String mdc=md.md5calc(pust_val+"flower_duigu2016");
			if(secret.endsWith(mdc)){
				List<Object> listid=new ArrayList<Object>();
				SimpleDateFormat sim=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String t=sim.format(new Date());
				GetIp getip=new GetIp();
				String ip=getip.getIp2(request);
				if(id==null){
					System.out.println("提醒用户id为空");
				}else{
					BuildString build=new BuildString();
					String s=build.StringBuilder(id);
					fq.setRemindUserId(s);
				}
				fq.setUserId(user_id);
				fq.setPv(0);
				fq.setIp(ip);
				fq.setCreateTime(Timestamp.valueOf(t));
				boolean b=helpService.addfaq(fq);
				if(myfiles==null){
					System.out.println("为空");
				}else{
					for(MultipartFile myfile : myfiles){  
						if(myfile.isEmpty()){  
							System.out.println("图片文件为空");
							return null;
						}else{  
							String fileName=myfile.getOriginalFilename();
							fileName = fileName.substring(fileName.lastIndexOf("\\")+1);
							String extName = fileName.substring(fileName.lastIndexOf("."));//.jpg
							String uuid = UUID.randomUUID().toString().replace("-", "");
							String newName = uuid+extName;   
							String realPath = request.getSession().getServletContext().getInitParameter("UPLOAD_IMAGE_PATH_FAQ");//从配置文件中读取要保存的位置 
							try {
								org.apache.commons.io.FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath,newName));
							} catch (IOException e) {
								e.printStackTrace();
							}
							String url="http://www.zhiwuxiansheng.com:8080/images/faqphoto/"+newName;
							UgcUploadImages faqp=new UgcUploadImages();
							faqp.setCreateTime(Timestamp.valueOf(t));
							faqp.setIp(ip);
							faqp.setFromContentType(3);
							faqp.setUrl(url);
							faqp.setUserId(fq.getUserId());
							faqp.setFromContentId(fq.getId());
							boolean bb=helpService.addfaqphoto(faqp);//---AAAA添加用户发布求助图片到 图片数据表
							if(bb){
								listid.add(faqp.getId());
							}else{
								System.out.println("图片未上传成功");
							}
						}  
					}
					UgcUploadImagesTags tag=new UgcUploadImagesTags();
					if(tag0!=null){
						int len=tag0.length;
						for (int i = 0; i < len/4; i++) {
							Integer direct=Integer.parseInt(tag0[4*i+3]);
							tag.setX(String.valueOf(tag0[4*i]));
							tag.setY(String.valueOf(tag0[4*i+1]));
							String text=tag0[4*i+2].toString();
							if(text.equals(null)){
								tag.setText("");
							}else{
								tag.setText(String.valueOf(tag0[4*i+2]));
							}
							tag.setRediret(direct);
							tag.setUserId(user_id);
							tag.setCreateTime(Timestamp.valueOf(t));
							tag.setIp(ip);
							tag.setFromImageId(Integer.parseInt(listid.get(0).toString()));
							tag.setClickNumber(0);
							helpService.AddTags(tag);//---添加标签
						}
					}
					if(tag1!=null){
						int len1=tag1.length;
						for (int i = 0; i < len1/4; i++) {
							Integer direct=Integer.parseInt(tag1[4*i+3]);
							tag.setX(String.valueOf(tag1[4*i]));
							tag.setY(String.valueOf(tag1[4*i+1]));
							String text=String.valueOf(tag1[4*i+2]);
							if(text.equals(null)){
								tag.setText("");
							}else{
								tag.setText(String.valueOf(tag1[4*i+2]));
							}
							tag.setRediret(direct);
							tag.setUserId(user_id);
							tag.setCreateTime(Timestamp.valueOf(t));
							tag.setIp(ip);
							tag.setFromImageId(Integer.parseInt(listid.get(1).toString()));
							tag.setClickNumber(0);
							helpService.AddTags(tag);//---添加标签
						}
					}
					if(tag2!=null){
						int len1=tag2.length;
						for (int i = 0; i < len1/4; i++) {
							Integer direct=Integer.parseInt(tag2[4*i+3]);
							tag.setX(String.valueOf(tag2[4*i]));
							tag.setY(String.valueOf(tag2[4*i+1]));
							String text=String.valueOf(tag2[4*i+2]);
							if(text.equals(null)){
								tag.setText("");
							}else{
								tag.setText(String.valueOf(tag2[4*i+2]));
							}
							tag.setRediret(direct);
							tag.setUserId(user_id);
							tag.setCreateTime(Timestamp.valueOf(t));
							tag.setIp(ip);
							tag.setFromImageId(Integer.parseInt(listid.get(2).toString()));
							tag.setClickNumber(0);
							helpService.AddTags(tag);//---添加标签
						}
					}
					if(tag3!=null){
						int len1=tag3.length;
						for (int i = 0; i < len1/4; i++) {
							Integer direct=Integer.parseInt(tag3[4*i+3]);
							tag.setX(String.valueOf(tag3[4*i]));
							tag.setY(String.valueOf(tag3[4*i+1]));
							String text=String.valueOf(tag3[4*i+2]);
							if(text.equals(null)){
								tag.setText("");
							}else{
								tag.setText(String.valueOf(tag3[4*i+2]));
							}
							tag.setRediret(direct);
							tag.setUserId(user_id);
							tag.setCreateTime(Timestamp.valueOf(t));
							tag.setIp(ip);
							tag.setFromImageId(Integer.parseInt(listid.get(3).toString()));
							tag.setClickNumber(0);
							helpService.AddTags(tag);//---添加标签
						}
					}
					if(tag4!=null){
						int len1=tag4.length;
						for (int i = 0; i < len1/4; i++) {
							Integer direct=Integer.parseInt(tag4[4*i+3]);
							tag.setX(String.valueOf(tag4[4*i]));
							tag.setY(String.valueOf(tag4[4*i+1]));
							String text=String.valueOf(tag4[4*i+2]);
							if(text.equals(null)){
								tag.setText("");
							}else{
								tag.setText(String.valueOf(tag4[4*i+2]));
							}
							tag.setRediret(direct);
							tag.setUserId(user_id);
							tag.setCreateTime(Timestamp.valueOf(t));
							tag.setIp(ip);
							tag.setFromImageId(Integer.parseInt(listid.get(4).toString()));
							tag.setClickNumber(0);
							helpService.AddTags(tag);//---添加标签
						}
					}
					if(tag5!=null){
						int len1=tag5.length;
						for (int i = 0; i < len1/4; i++) {
							Integer direct=Integer.parseInt(tag5[4*i+3]);
							tag.setX(String.valueOf(tag5[4*i]));
							tag.setY(String.valueOf(tag5[4*i+1]));
							String text=String.valueOf(tag5[4*i+2]);
							if(text.equals(null)){
								tag.setText("");
							}else{
								tag.setText(String.valueOf(tag5[4*i+2]));
							}
							tag.setRediret(direct);
							tag.setUserId(user_id);
							tag.setCreateTime(Timestamp.valueOf(t));
							tag.setIp(ip);
							tag.setFromImageId(Integer.parseInt(listid.get(5).toString()));
							tag.setClickNumber(0);
							helpService.AddTags(tag);//---添加标签
						}
					}
					if(tag6!=null){
						int len1=tag6.length;
						for (int i = 0; i < len1/4; i++) {
							Integer direct=Integer.parseInt(tag6[4*i+3]);
							tag.setX(String.valueOf(tag6[4*i]));
							tag.setY(String.valueOf(tag6[4*i+1]));
							String text=String.valueOf(tag6[4*i+2]);
							if(text.equals(null)){
								tag.setText("");
							}else{
								tag.setText(String.valueOf(tag6[4*i+2]));
							}
							tag.setRediret(direct);
							tag.setUserId(user_id);
							tag.setCreateTime(Timestamp.valueOf(t));
							tag.setIp(ip);
							tag.setFromImageId(Integer.parseInt(listid.get(6).toString()));
							tag.setClickNumber(0);
							helpService.AddTags(tag);//---添加标签
						}
					}
					if(tag7!=null){
						int len1=tag7.length;
						for (int i = 0; i < len1/4; i++) {
							Integer direct=Integer.parseInt(tag7[4*i+3]);
							tag.setX(String.valueOf(tag7[4*i]));
							tag.setY(String.valueOf(tag7[4*i+1]));
							String text=String.valueOf(tag7[4*i+2]);
							if(text.equals(null)){
								tag.setText("");
							}else{
								tag.setText(String.valueOf(tag7[4*i+2]));
							}
							tag.setRediret(direct);
							tag.setUserId(user_id);
							tag.setCreateTime(Timestamp.valueOf(t));
							tag.setIp(ip);
							tag.setFromImageId(Integer.parseInt(listid.get(7).toString()));
							tag.setClickNumber(0);
							helpService.AddTags(tag);//---添加标签
						}
					}
					if(tag8!=null){
						int len1=tag8.length;
						for (int i = 0; i < len1/4; i++) {
							Integer direct=Integer.parseInt(tag8[4*i+3]);
							tag.setX(String.valueOf(tag8[4*i]));
							tag.setY(String.valueOf(tag8[4*i+1]));
							String text=String.valueOf(tag8[4*i+2]);
							if(text.equals(null)){
								tag.setText("");
							}else{
								tag.setText(String.valueOf(tag8[4*i+2]));
							}
							tag.setRediret(direct);
							tag.setUserId(user_id);
							tag.setCreateTime(Timestamp.valueOf(t));
							tag.setIp(ip);
							tag.setFromImageId(Integer.parseInt(listid.get(8).toString()));
							tag.setClickNumber(0);
							helpService.AddTags(tag);//---添加标签
						}
					}
					if(b){
						if(id==null){
							List<String> alldevicetoken=new ArrayList<String>(); 
							Set<String> set=new HashSet<String>();
							UgcUser u=new UgcUser();
							u.setId(user_id);
							alldevicetoken=helpService.AllUserDeviceToken(u);
							for (String string : alldevicetoken) {
								set.add(string);
							}
							if(alldevicetoken.size()<1){
								System.out.println("推送所有用户token为空");
							}else{
								uf.setId(fq.getUserId());
								UgcUser uu=(UgcUser) userServic.OneUser(uf);
								String mes=SendMessage.Senfaq(set, uu.getNickname(),fq.getId());
								map.put("mes", mes);
							}
						}else{
							List<String> list=new ArrayList<String>();
							Set<String> set=new HashSet<String>();
							BuildString build=new BuildString();
							String ids=build.StringBuilder(id);
							list=helpService.UserFriendToken(ids);
							for (String string : list) {
								set.add(string);
							}
							if(list.size()<1){
								map.put("mes", "推送我的好友token为空");
							}else{
								uf.setId(fq.getUserId());
								UgcUser u=(UgcUser) userServic.OneUser(uf);
								String mes=SendMessage.Senfaq(set,u.getNickname(),fq.getId());
								map.put("mes", mes);
							}
						}
						map.put("mes", "发布求助成功");
						map.put("state", 1);
						return map;
					}else{
						map.put("message", "用户发布求助失败");
						map.put("state", 0);
						return map;
					}
				}
				map.put("message", "用户发布求助失败");
				map.put("state", 0);
				return map;
			}else{
				map.put("message", "秘钥错误");
				map.put("state", 0);
				return map;
			}
		}
	}

}
